# coding:utf-8
import xlrd
import os


class ExcelUtil():
    def __init__(self, excelPath, sheetName="Sheet"):
        self.data = xlrd.open_workbook(excelPath)
        self.table = self.data.sheet_by_name(sheetName)
        # 获取第一行作为key值
        self.keys = self.table.row_values(0)
        # 获取总行数
        self.rowNum = self.table.nrows
        # 获取总列数
        self.colNum = self.table.ncols

    def dict_data(self):
        if self.rowNum <= 1:
            print("总行数小于1")
        else:
            r = []
            j = 1
            for i in list(range(self.rowNum - 1)):
                s = {}
                # 从第二行取对应values值
                s['rowNum'] = i + 2
                values = self.table.row_values(j)
                for x in list(range(self.colNum)):
                    s[self.keys[x]] = values[x]
                r.append(s)
                j += 1
            return r


def run_success_result():
    curpath = os.path.dirname(os.path.realpath(__file__))
    report_path = os.path.join(os.path.dirname(curpath), 'report')
    reportxlsx = os.path.join(report_path, 'kuai8_result.xlsx')
    data = ExcelUtil(reportxlsx, sheetName='Sheet')
    run_count = len(data.dict_data())
    print(run_count)
    pass_count = 0
    for i in data.dict_data():
        if i['result'] == 'pass':
            pass_count += 1
        else:
            pass
    if run_count == pass_count:
        return True
    else:
        return False


if __name__ == "__main__":
    filepath = "/Users/chengyanan/Desktop/excelddtdriver/case/kuai8_api.xlsx"
    sheetName = "Sheet2"
    data = ExcelUtil(filepath, sheetName)
    for i in data.dict_data():
        print(i)
    # run_success_result()
